package com.leaf.lowcode.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebFilter(urlPatterns = "/boot/*",filterName = "loginFilter")
public class LoginFilter implements Filter {
    //加载时调用
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init loginFilter");
    }
    //请求被拦截时调用
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        System.out.println("当前请求路径为:"+request.getRequestURI());
        String username = request.getParameter("username");
        if("yehao".equalsIgnoreCase(username)){
            filterChain.doFilter(servletRequest,servletResponse);
        }else{
            //不放行，web应用resp.sendRedirect("/index.html");
            response.sendRedirect("/yh/index.html");
            return;
        }
    }
    @Override
    public void destroy() {
        System.out.println("destroy loginFilter");
    }
}
